#!/usr/bin/make -f

#export DH_VERBOSE=1
export NV_VERBOSE=1

# see FEATURE AREAS in dpkg-buildflags(1)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/pkg-info.mk
DPKG_EXPORT_BUILDTOOLS=1
include /usr/share/dpkg/buildtools.mk

PKG_CONFIG_XORG_XSERVER_LIBDIR	 = $(shell pkg-config --variable=libdir xorg-server)
CPPFLAGS		+= -DPKG_CONFIG_XORG_XSERVER_LIBDIR='\"$(PKG_CONFIG_XORG_XSERVER_LIBDIR)\"'

export PREFIX		 = /usr
export DO_STRIP		 =
export CC_ONLY_CFLAGS	 = $(CPPFLAGS)
export HOST_CC_ONLY_CFLAGS	 = $(CPPFLAGS)
# reproducible builds: instead of _out/$(uname)_($uname -m) use a fixed directory
export OUTPUTDIR	 = _out/debian

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
export DEBUG		 = 1
endif

export NV_USE_BUNDLED_LIBJANSSON = 0

include debian/rules.defs
NVIDIA_LEGACY		?=
NVIDIA_TESLA		?=
NVIDIA_RELEASE		 = $(DEB_VERSION_UPSTREAM)
empty			 =
variant			 =
variant_description	 =
ifneq (,$(NVIDIA_LEGACY))
variant			 = legacy-$(NVIDIA_LEGACY)xx))
variant_description	 = $(empty) ($(NVIDIA_LEGACY)xx legacy version)))
endif
ifneq (,$(NVIDIA_TESLA))
variant			 = tesla-$(NVIDIA_TESLA)
variant_description	 = $(empty) (Tesla $(NVIDIA_TESLA) version)
endif
ifeq (yes,$(NVIDIA_TESLA))
variant			 = tesla
variant_description	 = $(empty) (Tesla version)
endif
-variant		 = $(if $(variant),-$(variant))
nvidia			 = nvidia$(-variant)
nvidia_private		 = nvidia/$(if $(variant),$(variant),current)
watch_version		?= $(or $(filter-out yes,$(NVIDIA_TESLA)),$(NVIDIA_LEGACY))

ALL_CONTROL	:= $(wildcard debian/nvidia-settings.*)
VARIANT_CONTROL	:= $(wildcard debian/nvidia-settings$(-variant).*)
RENAME_CONTROL	 = $(filter-out $(VARIANT_CONTROL),$(ALL_CONTROL))
RENAMED_CONTROL	 = $(patsubst debian/nvidia-settings.%,debian/nvidia-settings$(-variant).%,$(RENAME_CONTROL))
TEMPLATES	:= $(wildcard debian/*.in)
AUTOGEN		+= $(patsubst %.in,%,$(TEMPLATES))
AUTOGEN		+= $(patsubst %.in,%,$(RENAMED_CONTROL))
AUTOGEN		+= debian/substvars
AUTOKEEP	 = debian/watch
AUTOCLEAN	 = $(filter-out $(AUTOKEEP),$(AUTOGEN))


%:
	dh $@

override_dh_auto_build: $(AUTOGEN)
	CC_ONLY_CFLAGS="$(CPPFLAGS)" dh_auto_build -O--parallel
	$(MAKE) -C samples

override_dh_auto_install:
	dh_auto_install --destdir=debian/tmp
	install -m 0755 samples/$(OUTPUTDIR)/nv-control-dpy debian/tmp/usr/bin/
	install -d -m 0755 debian/tmp/usr/share/icons/hicolor/128x128/apps/
	install -m 0644 doc/nvidia-settings.png debian/tmp/usr/share/icons/hicolor/128x128/apps/nvidia-settings$(-variant).png

override_dh_auto_clean:
	dh_auto_clean -- BUILD_GTK3LIB=1

execute_after_dh_auto_clean:
	$(MAKE) -C samples clean
	$(RM) -r doc/_out samples/_out src/_out src/libXNVCtrl/_out
	$(RM) $(AUTOCLEAN)
	$(MAKE) -f debian/rules $(AUTOKEEP)

override_dh_gencontrol:
	dh_gencontrol -- \
		-V'nvidia=$(nvidia)' \
		-V'nvidia:VariantDesc=$(variant_description)' \
		-V'nvidia:Version=$(NVIDIA_RELEASE)'

# used by dpkg-genchanges
debian/substvars:
	echo 'nvidia:VariantDesc=$(variant_description)' > $@


# Generating control files
debian/%:: debian/%.in debian/rules debian/rules.defs
	perl -p \
		-e 's{#VARIANT#}{$(-variant)}g;' \
		-e 's{#VARIANT_DESC#}{$(variant_description)}g;' \
		-e 's{#PRIVATE#}{$(nvidia_private)}g;' \
		-e 's{#WATCH_VERSION#}{$(watch_version)}g;' \
		< $< > $@

# Renaming control files for variant packaging
ifneq (,$(variant))
nvidia-settings$(-variant).%: nvidia-settings.%
	cp -a $< $@
endif
